import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 购物车处理
 * 
 * @author liyong
 * @version 1.0
 */
public class ShoppingCart extends HttpServlet {

	/**
	 * 将用户选择书目存入Session,并打印全部选择记录。
	 * 
	 * 
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		// 取得Session对象
		// 如果Session不存在，为本次会话创建此对象
		HttpSession session = req.getSession(true);
		Integer itemCount = (Integer) session.getAttribute("itemCount");
		// 如果session是新的
		if (itemCount == null)
			itemCount = new Integer(0);

		PrintWriter out = res.getWriter();
		res.setContentType("text/html");

		// 接收传来的参数
		String[] itemsSelected;
		String itemName;
		itemsSelected = req.getParameterValues("item");

		if (itemsSelected != null) {
			for (String s : itemsSelected) {
				itemName = s;
				System.out.println(itemName);
				itemCount = itemCount + 1;
				// 购买的条目
				session.setAttribute("item" + itemCount, itemName);
				// 总条目
				session.setAttribute("itemCount", itemCount);
			}
		}

		out.println("<html>");
		out.println("<title>");
		out.println("item list");
		out.println("</title>");
		out.println("<body><h4>Books Added</h4><hr><br><br>");
		for (int i = 1; i <= itemCount; i++) {
			out.println((String) session.getAttribute("item" + i) + "<hr>");
		}
		out.println("</body>");
		out.println("</html>");
		out.close();
	}
}